package com.heima.reggie.common;

import org.springframework.context.annotation.Bean;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.sql.SQLIntegrityConstraintViolationException;

@RestControllerAdvice  //异常通知类注释
public class GlobalExceptionHandler {
//统一处理此类异常
    @ExceptionHandler(SQLIntegrityConstraintViolationException.class)
    public R ExceptionHandler(SQLIntegrityConstraintViolationException ex){
                                        //是否含有xxx 下为错误信息,重复
        if (ex.getMessage().contains("Duplicate entry")) {
            String[] s = ex.getMessage().split(" ");
            String msg=s[2]+"存在了";
            return R.error(msg);

        }


        return R.error("账户异常");
    }

    @ExceptionHandler(deleteException.class)
public R Delete(deleteException deleteException){


        return R.error(deleteException.getMessage());
}

}
